﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.IO.IsolatedStorage;

namespace Oiler
{
    public class OilDeposits
    {
        public string stats;
        public string name;

        private float _oilAmount;
        private double _taxRate;
        private int _lowerOilRigs;
        private int _medOilRigs;
        private int _highOilRigs;
        private double _income;
        private bool _isUsable;
        private int _sellableOil;
        private int _upkeep;

        public int lowOilRigCost = 450000;
        public int medOilRigCost = 950000;
        public int highOilRigCost = 2100000;
        public int arrPos;

        Texture2D statsPanel;
        Texture2D dot;
        Vector2 stringPosition;
        MapControl Map;

        public float SetOilAmount
        {
            get { return _oilAmount; }
            set { _oilAmount += value; }
        }
        public double SetTaxRate
        {
            get { return _taxRate; }
            set { _taxRate = value; }
        }
        public int LowerOilRigs
        {
            get { return _lowerOilRigs; }
            set { _lowerOilRigs += value; }
        }
        public int MedOilRigs
        {
            get { return _medOilRigs; }
            set { _medOilRigs += value; }
        }
        public int HighOilRigs
        {
            get { return _highOilRigs; }
            set { _highOilRigs += value; }
        }
        public double Income
        {
            get { return _income; }
            set { _income = value; }
        }
        public bool isUsable
        {
            get { return _isUsable; }
            set { _isUsable = value; }
        }
        public int SellableOil
        {
            get { return _sellableOil; }
            set { _sellableOil = value; }
        }
        public int Upkeep
        {
            get { return _upkeep; }
            set { _upkeep = value; }
        }

        public OilDeposits(int oilAmount, double taxRate, bool isUsable, string name, int ArrPos)
        {
            SetOilAmount = oilAmount;
            SetTaxRate = taxRate;
            this.isUsable = isUsable;
            this.name = name;
            arrPos = ArrPos;
        }

        public void Initialize(Texture2D Texture, Texture2D Dot)
        {
            statsPanel = Texture;
            dot = Dot;
        }
        
        public void Update(Vector2 oilPos, MapControl map, IsolatedStorageSettings saveGame, bool isGameRunning, Player player)
        {
            

            //maintenance costs of oil rigs
            Upkeep = (LowerOilRigs * 8000) + (MedOilRigs * 24000) + (HighOilRigs * 64000);

            //amount of oil that is ready to sell
            SellableOil = LowerOilRigs * 250 + MedOilRigs * 700 + HighOilRigs * 1800;

            //Income for this entire deposit
            Income = (SellableOil * player.BarrelPrice); //income is oil on hand x price per barrel minus upkeep of oil rigs
           

            Map = map;

            if (map.mapScale < 1)
            {
                stats = name;
                stringPosition = new Vector2(oilPos.X + 75 * map.mapScale, oilPos.Y + 5 * map.mapScale);
            }
            else
            {
                stats = name + "\n" + LowerOilRigs + " " + MedOilRigs + " " + HighOilRigs + " " + SetOilAmount;
                stringPosition = new Vector2(oilPos.X + 75 * map.mapScale, oilPos.Y - 20 * map.mapScale);
            }

                
            
        }

        public void Draw(SpriteBatch spriteBatch, SpriteFont GameFont, Vector2 oilPos, MapControl map)
        {
            if(isUsable)
            {
                spriteBatch.Draw(statsPanel, new Vector2(oilPos.X + 65 * map.mapScale, oilPos.Y - 25 * map.mapScale), null, Microsoft.Xna.Framework.Color.White, 0f, Vector2.Zero, map.mapScale, SpriteEffects.None, 0f);
                spriteBatch.DrawString(GameFont, stats, stringPosition, Microsoft.Xna.Framework.Color.Black, 0f, Vector2.Zero, map.mapScale, SpriteEffects.None, 0f);
            }
        }

        public Microsoft.Xna.Framework.Rectangle HitBounds
        {
            
            get
            {
                Microsoft.Xna.Framework.Rectangle r = new Microsoft.Xna.Framework.Rectangle
                    ((int)Map.depositPos[arrPos].X, (int)Map.depositPos[arrPos].Y, (int)dot.Width, (int)dot.Height);

                return r;
            }
        }

        public void Save(IsolatedStorageSettings saveGame)
        {
            if (!saveGame.Contains("low" + arrPos))
                saveGame.Add(("low" + arrPos), LowerOilRigs);
            else
                saveGame[("low" + arrPos)] = LowerOilRigs;

            if (!saveGame.Contains("med" + arrPos))
                saveGame.Add(("med" + arrPos), MedOilRigs);
            else
                saveGame[("med" + arrPos)] = MedOilRigs;
            
            if (!saveGame.Contains("high" + arrPos))
                saveGame.Add(("high" + arrPos), HighOilRigs);
            else
                saveGame[("high" + arrPos)] = MedOilRigs;

            if (!saveGame.Contains("oil" + arrPos))
                saveGame.Add(("oil" + arrPos), SetOilAmount);
            else
                saveGame[("oil" + arrPos)] = SetOilAmount;

            saveGame.Save();
        }

        public void Load(IsolatedStorageSettings saveGame)
        {
            if (saveGame.Contains(("low" + arrPos)))
                LowerOilRigs = (int)saveGame[("low" + arrPos)];

            if (saveGame.Contains(("med" + arrPos)))
                MedOilRigs = (int)saveGame[("med" + arrPos)];

            if (saveGame.Contains(("high" + arrPos)))
                HighOilRigs = (int)saveGame[("high" + arrPos)];

            if (saveGame.Contains(("oil" + arrPos)))
                SetOilAmount = (float)saveGame[("oil" + arrPos)];
        }
    }
}
